﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WikiTrailCreator.TrailImages
{
    public partial class FormImageSelector : Form
    {
        private BindingList<TrailImage> trailImagesListItems = new BindingList<TrailImage>();

        public FormImageSelector()
        {
            InitializeComponent();

            listBoxImages.DataSource = trailImagesListItems;
            listBoxImages.DisplayMember = "DisplayText";
        }

        private void listBoxImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            TrailImage selectedItem = listBoxImages.SelectedItem as TrailImage;

            if (selectedItem != null)
            {
                labelImageBox.Image = selectedItem.Image;
            }
            else
            {
                labelImageBox.Image = null;
            }
        }

        public TrailImage SelectedImage
        {
            get
            {
                return listBoxImages.SelectedItem as TrailImage;
            }
        }

        public TrailImage[] SelectedImages
        {
            get
            {
                return listBoxImages.SelectedItems.Cast<TrailImage>().ToArray();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Returns the wiki code for the selected images as thumbnails.
        /// </summary>
        public string GetInternalThumbnailCode()
        {
            string wikiCode = "";
            
            TrailImage[] trailImages = this.SelectedImages;

            foreach (TrailImage trailImage in trailImages)
            {
                wikiCode += "<thumbnail id=\"" + trailImage.Id + "\"></thumbnail>" + Environment.NewLine;
            }

            return wikiCode;
        }

        private void FormImageSelector_Shown(object sender, EventArgs e)
        {
            // Initialize image list...
            trailImagesListItems = new BindingList<TrailImage>();
            foreach (TrailImage trailImage in GlobalImageGalleryContainer.Instance.Images)
            {
                trailImagesListItems.Add(trailImage);
            }

            // Bind image list...
            listBoxImages.DataSource = trailImagesListItems;
            listBoxImages.DisplayMember = "PhotoName";
        }
    }
}
